<?php
require_once dirname(__FILE__).DIRECTORY_SEPARATOR.'api'.DIRECTORY_SEPARATOR.'sdk.class.php';
require_once dirname(__FILE__).DIRECTORY_SEPARATOR.'lib'.DIRECTORY_SEPARATOR.'firstclass.php';
require_once dirname(__FILE__).DIRECTORY_SEPARATOR.'function'.DIRECTORY_SEPARATOR.'aliyunoss_watermark_do.php';
require_once dirname(__FILE__).DIRECTORY_SEPARATOR.'function'.DIRECTORY_SEPARATOR.'aliyunoss_img2thumb.php';
require_once dirname(__FILE__).DIRECTORY_SEPARATOR.'function'.DIRECTORY_SEPARATOR.'aliyunoss_remoteimage.php';
RegisterPlugin("AliyunOSS","ActivePlugin_AliyunOSS");
if ($zbp->Config('AliyunOSS')->HasKey('version')) {
	define('OSS_ACCESS_ID', $zbp->Config('AliyunOSS')->access_id);	//ACCESS_ID
	define('OSS_ACCESS_KEY', $zbp->Config('AliyunOSS')->access_key);	//ACCESS_KEY
	define('OSS_DEFAULT_HOST', $zbp->Config('AliyunOSS')->default_host);	//OSS_HOST
	//绑定域名
	if ($zbp->Config('AliyunOSS')->object_url != '') {
		define('OSS_OBJECT_URL', $zbp->Config('AliyunOSS')->object_url);
	}elseif ($zbp->Config('AliyunOSS')->default_host == 'oss-cn-qingdao.aliyuncs.com' || $zbp->Config('AliyunOSS')->default_host == 'oss-cn-qingdao-internal.aliyuncs.com') {
		define('OSS_OBJECT_URL', 'http://' . $zbp->Config('AliyunOSS')->bucket . '.oss-cn-qingdao.aliyuncs.com/');
		# code...
	}elseif ($zbp->Config('AliyunOSS')->default_host == 'oss-cn-hangzhou.aliyuncs.com' || $zbp->Config('AliyunOSS')->default_host == 'oss-cn-hangzhou-internal.aliyuncs.com') {
		define('OSS_OBJECT_URL', 'http://' . $zbp->Config('AliyunOSS')->bucket . '.oss-cn-hangzhou.aliyuncs.com/');
		# code...
	}elseif ($zbp->Config('AliyunOSS')->default_host == 'oss-cn-beijing.aliyuncs.com' || $zbp->Config('AliyunOSS')->default_host == 'oss-cn-beijing-internal.aliyuncs.com') {
		define('OSS_OBJECT_URL', 'http://' . $zbp->Config('AliyunOSS')->bucket . '.oss-cn-beijing.aliyuncs.com/');
		# code...
	}elseif ($zbp->Config('AliyunOSS')->default_host == 'oss-cn-xianggang.aliyuncs.com' || $zbp->Config('AliyunOSS')->default_host == 'oss-cn-xianggang-internal.aliyuncs.com') {
		define('OSS_OBJECT_URL', 'http://' . $zbp->Config('AliyunOSS')->bucket . '.oss-cn-xianggang.aliyuncs.com/');
		# code...
	}

	if ($zbp->Config('AliyunOSS')->default_host == 'oss-cn-qingdao.aliyuncs.com' || $zbp->Config('AliyunOSS')->default_host == 'oss-cn-qingdao-internal.aliyuncs.com') {
		define('OSS_OBJECT_URL1', 'http://' . $zbp->Config('AliyunOSS')->bucket . '.oss-cn-qingdao.aliyuncs.com/');
		# code...
	}elseif ($zbp->Config('AliyunOSS')->default_host == 'oss-cn-hangzhou.aliyuncs.com' || $zbp->Config('AliyunOSS')->default_host == 'oss-cn-hangzhou-internal.aliyuncs.com') {
		define('OSS_OBJECT_URL1', 'http://' . $zbp->Config('AliyunOSS')->bucket . '.oss-cn-hangzhou.aliyuncs.com/');
		# code...
	}elseif ($zbp->Config('AliyunOSS')->default_host == 'oss-cn-beijing.aliyuncs.com' || $zbp->Config('AliyunOSS')->default_host == 'oss-cn-beijing-internal.aliyuncs.com') {
		define('OSS_OBJECT_URL1', 'http://' . $zbp->Config('AliyunOSS')->bucket . '.oss-cn-beijing.aliyuncs.com/');
		# code...
	}elseif ($zbp->Config('AliyunOSS')->default_host == 'oss-cn-xianggang.aliyuncs.com' || $zbp->Config('AliyunOSS')->default_host == 'oss-cn-xianggang-internal.aliyuncs.com') {
		define('OSS_OBJECT_URL1', 'http://' . $zbp->Config('AliyunOSS')->bucket . '.oss-cn-xianggang.aliyuncs.com/');
		# code...
	}

//	define('OSS_OBJECT_URL', $zbp->Config('AliyunOSS')->object_url);
	define('OSS_OBJECT_SAVECATA', $zbp->Config('AliyunOSS')->object_cate);	//储存目录

	define('THUMB_WIDTH', $zbp->Config('AliyunOSS')->thumb_width);	//宽度
	define('THUMB_HEIGHT', $zbp->Config('AliyunOSS')->thumb_height);	//高度
	define('THUMB_CUT', $zbp->Config('AliyunOSS')->thumb_cut);	//裁剪
	define('THUMB_PROPORTION', $zbp->Config('AliyunOSS')->thumb_proportion);	//缩放

	$bucket = $zbp->Config('AliyunOSS')->bucket; //bucket
	$obj = new ALIOSS();
	$obj->set_debug_mode(FALSE);
	$obj->set_enable_domain_style(TRUE);	//二级域名
}

function ActivePlugin_AliyunOSS() {
	Add_Filter_Plugin('Filter_Plugin_Admin_TopMenu','AliyunOSS_AddMenu');
	Add_Filter_Plugin('Filter_Plugin_Upload_Url','AliyunOSS_upload_url');
	Add_Filter_Plugin('Filter_Plugin_Upload_SaveFile','AliyunOSS_upload_savefile');
	Add_Filter_Plugin('Filter_Plugin_Upload_DelFile','AliyunOSS_upload_delfile');
	Add_Filter_Plugin('Filter_Plugin_Upload_SaveBase64File','AliyunOSS_upload_savefile');
	Add_Filter_Plugin('Filter_Plugin_PostArticle_Core','AliyunOSS_RemoteImage');
}

function AliyunOSS_AddMenu(&$m){
	global $zbp;
	$m[]=MakeTopMenu("root",'阿里云存储',$zbp->host . "zb_users/plugin/AliyunOSS/main.php","","topmenu_AliyunOSS");
}

function AliyunOSS_RemoteImage(&$article) {
	global $zbp;

	set_time_limit(0);
	ZBlogException::ClearErrorHook();
	$content = $article->Content;
	$pattern = "/<[img|IMG].*?src=[\'|\"](.*?(?:[\.gif|\.jpg|\.png]))[\'|\"].*?[\/]?>/";
	preg_match_all($pattern,$content,$matchContent);
	$picArray = array_unique($matchContent[1]);
	if ($picArray){		
		foreach($picArray as $key=>$rurl){
			if(substr($rurl,0,strlen(OSS_OBJECT_URL))!=OSS_OBJECT_URL) {
				$file_path = $zbp->usersdir . 'upload/tmp.data.' . rand(10000,99999) . time();
				$file_name=date('YmdHis').'_'.rand(10000,99999).'.'.pathinfo($rurl,PATHINFO_EXTENSION);
				$object = OSS_OBJECT_SAVECATA . date("Y/m/", time()) . $file_name;	//构造云文件名
				$getpic = AliyunOSS_RemoteImage_Save($rurl,$file_path,$file_name,$object);
				$picUrl = OSS_OBJECT_URL . $object;
				$article->Content = str_replace($rurl,$picUrl,$article->Content);
			}
		}
	}
}

function AliyunOSS_upload_url(&$upload) {
	global $zbp;
	$file = $zbp->GetUploadByID($upload->ID);
	$url = OSS_OBJECT_URL . $file->Metas->AliyunOSS_URL;
	return $url;
}

function AliyunOSS_upload_delfile(&$upload) {
	global $zbp,$obj,$bucket;

	$object = $upload->Metas->AliyunOSS_URL;
	$obj->delete_object($bucket,$object);
	return true;
	$GLOBALS['Filter_Plugin_Upload_DelFile']['CloudStorage_Del'] = PLUGIN_EXITSIGNAL_RETURN;
}

function AliyunOSS_upload_savefile($tmp, &$upload) {
	global $zbp,$obj,$bucket;

	if($zbp->Config('AliyunOSS')->water_enable) {
		$waterImage=$zbp->path . 'zb_users/plugin/AliyunOSS/img/watermark.png'; //作为水印的图片，暂只支持GIF,JPG,PNG格式；
		$waterPos =$zbp->Config('AliyunOSS')->waterPos; //水印位置
		$waterText=$zbp->Config('AliyunOSS')->waterText;//文字水印，即把文字作为为水印；
		$textFont =$zbp->Config('AliyunOSS')->textFont;//文字大小；
		$textColor=$zbp->Config('AliyunOSS')->textColor;//文字颜色，值为十六进制颜色值，默认为#FF0000(红色)；
		$watermarkType=$zbp->Config('AliyunOSS')->watermarkType;
		$tmp_info = getimagesize($tmp);
		if($tmp_info[2]>0 and  $tmp_info[2]<4) {
			AliyunOSS_Watermark_Do($tmp,$waterPos,$waterImage,$waterText,$textFont,$textColor,$watermarkType);
		}
	}

	$file_path = $zbp->usersdir . 'upload/tmp.data.' . time();
	$file_name = date('YmdHis') . '_' . mt_rand(10000, 99999) . '.' . GetFileExt($upload->SourceName);
	$object= OSS_OBJECT_SAVECATA . date("Y/m/", time()) . $file_name;	//构造云文件名
	
	if (is_file($tmp)) {
		@move_uploaded_file($tmp, $file_path);//先上传到本地
	}else{
		@file_put_contents($file_path, base64_decode($tmp));
	}
	
	$upload->Name = $file_name;	

	$response = $obj->upload_file_by_file($bucket,$object,$file_path);//将本地文件上传到云
	$upload->Metas->AliyunOSS_URL = $object;
	
	unlink($file_path);

	$GLOBALS['Filter_Plugin_Upload_SaveFile']['AliyunOSS_upload_savefile'] = PLUGIN_EXITSIGNAL_RETURN;
	$GLOBALS['Filter_Plugin_Upload_SaveBase64File']['AliyunOSS_upload_savefile'] = PLUGIN_EXITSIGNAL_RETURN;

	return true;
}

function InstallPlugin_AliyunOSS() {
    global $zbp,$obj,$bucket;
    //配置初始化
    if (!$zbp->Config('AliyunOSS')->HasKey('version')) {
        $zbp->Config('AliyunOSS')->version = '1.0';

        $zbp->Config('AliyunOSS')->access_id = 'JLJjH2C8DZfMfsAt';
		$zbp->Config('AliyunOSS')->access_key = 'sdjfslkfljfejflmwflewnjfew';
		$zbp->Config('AliyunOSS')->default_host = 'oss-cn-qingdao-internal.aliyuncs.com';
		$zbp->Config('AliyunOSS')->object_url = 'http://images.fengwensheng.com/';
		$zbp->Config('AliyunOSS')->object_cate = 'zb_users/upload/';
		$zbp->Config('AliyunOSS')->bucket = 'xafws';

		$zbp->Config('AliyunOSS')->water_enable = flase;
		$zbp->Config('AliyunOSS')->waterPos = '9';
		$zbp->Config('AliyunOSS')->waterText = '来源：http://fengwensheng.com';
		$zbp->Config('AliyunOSS')->textFont = '12';
		$zbp->Config('AliyunOSS')->textColor = '#FF0000';
		$zbp->Config('AliyunOSS')->watermarkType = 'image';

		$zbp->Config('AliyunOSS')->thumb_height = '179';
    	$zbp->Config('AliyunOSS')->thumb_width = '229';
    	$zbp->Config('AliyunOSS')->thumb_cut = ture;
    	$zbp->Config('AliyunOSS')->thumb_proportion = '0';

        $zbp->SaveConfig('AliyunOSS');
    }
}

function UninstallPlugin_AliyunOSS() {
	global $zbp;
//	$zbp->DelConfig('AliyunOSS');
}